package cn.nawang.ebeim.ui.controller;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.client.bean.NetWorkMonitorBean;
import cn.nawang.ebeim.client.util.NetWorkMonitorDbUtil;
import cn.nawang.ebeim.ui.bean.DetailPlan;
import cn.nawang.ebeim.ui.bean.User;
import cn.nawang.ebeim.ui.service.MonitorService;
import cn.nawang.ebeim.ui.util.ApiUrl;
import cn.nawang.ebeim.ui.util.MACAddress;
import cn.nawang.ebeim.ui.util.PcInfoUtil;
import cn.nawang.ebeim.ui.util.XmlUtil;

@RestController
public class MonitorController {
  private static final Logger LOG = LoggerFactory.getLogger(MonitorController.class);


  @Autowired
  private HttpSession session;

  @Autowired
  private MonitorService monitorService;

  /**
   * 
   * @author: huanghj
   * @Description: 查看当前数据源运行状况
   * @param @param mac
   * @param @param isMac
   * @param @return
   * @param @throws UnsupportedEncodingException
   * @return String
   * @date 2015年11月10日下午6:30:17
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/monitor/datasources/list", method = RequestMethod.GET)
  public String getDataSourcesByCreateUserIdList(String mac, String isMac)
      throws UnsupportedEncodingException {
    LOG.debug("/monitor/datasources/list {}", mac, isMac);
    String terminalId = MACAddress.getMACAddr();
    User user = (User) session.getAttribute("user");
    String data = ApiUrl.getDatasourceList(user.getId(), terminalId, isMac);
    JSONObject json = JSONObject.parseObject(data);
    json.put("userSpace", user.getSpaceSize());
    json.put("mac", terminalId);
    return json.toString();
  }

  /**
   * 
   * @author: huanghj
   * @Description: 获取软件监控信息
   * @param @param mac
   * @param @param isMac
   * @param @return
   * @param @throws UnsupportedEncodingException
   * @param @throws Exception
   * @return String
   * @date 2015年11月23日下午4:07:23
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/monitor/userInfo", method = RequestMethod.GET)
  public String getUserInfo(String mac, String isMac) {
    LOG.debug("/monitor/userInfo {}", mac, isMac);
    User user = (User) session.getAttribute("user");
    String data = ApiUrl.getUserInfoPart(user.getId(), user.getSpaceSize(), user.getLabId());
    JSONObject json = JSONObject.parseObject(data);
    try {
      json.put("code", 200);
      json.put("bindDevice", MACAddress.getMACAddr());
      Map map = XmlUtil.getServiceInfo(XmlUtil.serverPath);
      json.put("bindDeviceName", MACAddress.getComputerName());
      json.put("netcircums", PcInfoUtil.ethernet());
      LOG.debug("========================" + PcInfoUtil.ethernet());
      json.put("mem", PcInfoUtil.memory() + " G");
      json.put("systemOpt", PcInfoUtil.getSystemInfo());
      json.put("processor", PcInfoUtil.cpuProcess());
      json.put("serviceInfo", map);
    } catch (Exception e) {
      json.put("code", 500);
      e.printStackTrace();
    }
    return json.toString();
  }


  @RequestMapping(value = "/monitor/cpuRate", method = RequestMethod.GET)
  public String rate() throws UnsupportedEncodingException, Exception {
    DecimalFormat df = new DecimalFormat("0.00");
    JSONObject json = new JSONObject();
    String tt = PcInfoUtil.getCpuRate();
    json.put("cpuRate", tt);
    json.put("memRate", df.format(PcInfoUtil.memoryRate()));
    return json.toString();
  }

  /**
   * 
   * @author: huanghj
   * @Description: 获取数据源（文件）执行计划列表
   * @param @param nativeSel
   * @param @param useSel
   * @param @param onlineSel
   * @param @return
   * @param @throws UnsupportedEncodingException
   * @return String
   * @date 2015年11月23日下午4:06:37
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/monitor/datasources/plan", method = RequestMethod.GET)
  public String getDataSourcesPlanByUserId(String nativeSel, String useSel, String onlineSel)
      throws UnsupportedEncodingException {
    LOG.debug("/monitor/datasources/plan {}", nativeSel, useSel, onlineSel);
    String terminalId = MACAddress.getMACAddr();
    User user = (User) session.getAttribute("user");
    String data = ApiUrl.getMonitorPlan(user.getId(), terminalId, nativeSel, useSel, onlineSel);
    JSONObject json = JSONObject.parseObject(data);
    return json.toString();
  }

  /**
   * 
   * @author: huanghj
   * @Description: 获取预警事件列表
   * @param @param type
   * @param @param level
   * @param @param begin
   * @param @param end
   * @param @return
   * @param @throws UnsupportedEncodingException
   * @return String
   * @date 2015年11月23日下午4:05:51
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/monitor/warnList", method = RequestMethod.GET)
  public String getWarnListByUserId(String type, String level, String begin, String end)
      throws UnsupportedEncodingException {
    LOG.debug("/monitor/warnList {}", type, level, begin, end);
    User user = (User) session.getAttribute("user");
    String data = ApiUrl.getWarnList(user.getId(), type, level, begin, end);
    JSONObject json = JSONObject.parseObject(data);
    return json.toString();
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取网络监控信息
   * @return String
   * @date Nov 19, 20159:09:32 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/monitor/net_work_monitor", method = RequestMethod.GET)
  public String getNetWorkMonitor() {
    LOG.debug("/monitor/net_work_monitor {}");
    JSONObject json = new JSONObject();
    json.put("code", "500");
    try {
      NetWorkMonitorDbUtil.initConnection();
      NetWorkMonitorDbUtil.initDatabase();
      List<NetWorkMonitorBean> list = NetWorkMonitorDbUtil.getNetWorkMonitorBean();
      if (list != null) {
        json.put("list", list);
        json.put("desc", "获取成功");
        json.put("code", 200);
      } else {
        json.put("code", 400);
        json.put("desc", "请稍后，还未获取到监控信息");
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return json.toString();
  }

  /**
   * 
   * @author: huanghj
   * @Description: 更改服务参数
   * @param @param status
   * @param @param name
   * @param @return
   * @param @throws UnsupportedEncodingException
   * @return String
   * @date 2015年11月23日下午4:05:24
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/monitor/changeServiceParam", method = RequestMethod.GET)
  public String getWarnListByUserId(String status, String name)
      throws UnsupportedEncodingException {
    JSONObject json = new JSONObject();
    boolean bool = XmlUtil.modifySon(XmlUtil.serverPath, name, status);
    if (bool) {
      json.put("code", 200);
      json.put("status", status);
    } else {
      json.put("code", 500);
    }
    json.put("tagName", name);
    return json.toString();
  }

  /**
   * 
   * @author: huanghj
   * @Description: 定期获取服务启动信息
   * @param @return
   * @param @throws UnsupportedEncodingException
   * @return String
   * @date 2015年11月23日下午4:04:59
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/monitor/getServiceStatus", method = RequestMethod.GET)
  public String getServiceStatus() throws UnsupportedEncodingException {
    JSONObject json = new JSONObject();
    Map map;
    try {
      map = XmlUtil.getServiceInfo(XmlUtil.serverPath);
      json.put("serviceInfo", map);
      json.put("code", 200);
    } catch (Exception e) {
      json.put("code", 500);
      e.printStackTrace();
    }
    return json.toString();
  }

  /**
   * 
   * @author: huanghj
   * @Description: 获取近十天的执行计划详细表
   * @param @param plan
   * @param @return
   * @return String
   * @date 2015年11月23日下午6:28:37
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/monitor/getDetailPlan", method = RequestMethod.GET)
  public String getDetailPlan(DetailPlan plan) {
    JSONObject json = new JSONObject();
    Map map = monitorService.getDetailPlan(plan);
    json.put("plan", map);
    json.put("code", 200);
    return json.toString();
  }

}

